<?php

namespace app\admin\model\xmwms;

use think\Model;

/**
 * WarehousingOrder
 */
class WarehousingOrder extends Model
{
    // 表名
    protected $name = 'wms_warehousing_order';

    /**
     * @var string 自动写入时间戳字段
     */
    protected $autoWriteTimestamp = true;

    protected $append = [
        'type_text',
        'audit_status_text'
    ];

    // 字段类型转换
    protected $type = [
        'updatetime' => 'timestamp:Y-m-d H:i:s',
        'deletetime' => 'timestamp:Y-m-d H:i:s',
    ];

    public function getTypeTextAttr($value, $row): string
    {
        if ($row['type'] === '' || $row['type'] === null) return '';
        switch ($row['type']) {
            case 10:
                $value = '采购入库';
                break;
            case 20:
                $value = '外协入库';
                break;
            case 30:
                $value = '退货入库';
                break;
            default:
                $value = '';
        }
        return $value;
    }

    public function getAuditStatusTextAttr($value, $row): string
    {
        if ($row['audit_status'] === '' || $row['audit_status'] === null) return '';
        $row['audit_status'] == 1 ? $value = '已审核' : $value = '未审核';
        return $value;
    }

    public function wmsSupplier()
    {
        return $this->belongsTo(\app\admin\model\xmwms\Supplier::class, 'supplier_id', 'id');
    }

    public function admin()
    {
        return $this->belongsTo(\app\admin\model\Admin::class, 'admin_id', 'id');
    }

    public function warehouseOrderDetail()
    {
        return $this->hasMany(WarehousingOrderDetail::class, 'receipt_no', 'receipt_no');
    }

}